package vim.meet.javassist;

import javassist.*;
import org.curitis.service.impl.BusinessServiceImpl;

public class Javasist {

    public static void main(String[] args) throws Throwable {
        ClassPool cp = ClassPool.getDefault();
        Loader cl = new Loader();
        cl.addTranslator(cp, new MyTranslator());
        BusinessServiceImpl businessService = new BusinessServiceImpl();
        System.out.println(businessService.doSomething(1,"curitis"));
    }


    public static class MyTranslator implements Translator {

        public void start(ClassPool pool) throws NotFoundException, CannotCompileException {

        }

        public void onLoad(ClassPool pool, String classname) {
            try {
                CtClass cc = pool.get(classname);
                CtMethod m = cc.getDeclaredMethod("doSomething");
                m.insertBefore("{ System.out.println(\"记录日志\"); }");
            } catch (NotFoundException e) {
            } catch (CannotCompileException e) {

            }
        }

    }
}
